{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "65d5c573-878c-426c-b0e2-e8953ce81706",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import html\n",
    "from urllib.parse import quote,unquote"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "50513bbd-8f45-4632-b876-6402ad9c277a",
   "metadata": {},
   "outputs": [],
   "source": [
    "str_raw = '美帝国<主义>，{都是*_*?纸老虎！'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0165663b-43d4-42c0-9243-bf8ca4848523",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'美帝国<主义>，\\\\{都是\\\\*_\\\\*\\\\?纸老虎！'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一：re.escape() 方法\n",
    "str_re = re.escape(str_raw)\n",
    "str_re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d8d665bd-b8ab-4424-a698-f61cfd69e75f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'美帝国&lt;主义&gt;，{都是*_*?纸老虎！'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法二：html.escape() 方法\n",
    "str_html = html.escape(str_raw,quote=True)\n",
    "str_html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3f558a84-3cfb-4f3c-a9f5-2e430e1e1667",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'%E7%BE%8E%E5%B8%9D%E5%9B%BD%3C%E4%B8%BB%E4%B9%89%3E%EF%BC%8C%7B%E9%83%BD%E6%98%AF%2A_%2A%3F%E7%BA%B8%E8%80%81%E8%99%8E%EF%BC%81'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法三：urllib.parse.quote() 方法\n",
    "str_urllib = quote(str_raw, encoding='utf-8')\n",
    "str_urllib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a5e92533-4783-4ae3-9900-0ccc382ab6ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'美帝国<主义>，{都是*_*?纸老虎！'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 urllib.parse.unquote() 方法 解码\n",
    "str_decode = unquote(str_urllib, encoding='utf-8')\n",
    "str_decode"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
